<template>
  <el-row class="login-container">
    <el-col :lg="16" ms="8" class="left">
      <div>
        <div>欢迎光临</div>
        <div>此站点是(vue3+vite实战商城后台开发)</div>
      </div>
    </el-col>
    <el-col :lg="8" ms="8" class="right">
      <h2 class="title">欢迎回来</h2>
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>
      <div>
        <el-form :model="form" class="w-[250px]" :rules="rules" ref="formRef">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名">
              <template #prefix>
                <el-icon class="el-input__icon"><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="密 码" prop="password">
            <el-input
              type="password"
              v-model="form.password"
              placeholder="请输入密码"
              class="ml-2"
              show-password
              @keyup.enter.native="onSubmit"
            >
              <template #prefix>
                <el-icon class="el-input__icon"><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="onSubmit"
              class="w-[250px]"
              round
              color="#626aef"
              :loading="loading"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, reactive } from "vue";
import { login } from "~/api/manager";
import { useRouter } from "vue-router";
import { setToken } from "~/composables/auth";
import { toast } from "~/composables/util";

const router = useRouter();

const form = reactive({
  username: "",
  password: "",
});

const rules = {
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
};

const formRef = ref(null);
const loading = ref(false);

const onSubmit = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return false;
    }
    loading.value = true;
    login(form.username, form.password)
      .then((res) => {
        console.log(res);
        //提示成功
        toast("登录成功");
        //存储token
        setToken(res.token);
        //跳转到后台主页
        router.push("/");
      })
      .finally(() => {
        loading.value = false;
      });
  });
};
</script>

<style scoped>
.login-container {
  @apply min-h-screen bg-indigo-500;
}
.login-container .left {
  @apply flex items-center justify-center;
}
.login-container .right {
  @apply bg-light-50 flex items-center justify-center flex-col;
}
.left > div > div:first-child {
  @apply font-bold text-5xl text-light-50 mb-4;
}
.left > div > div:last-child {
  @apply text-gray-200 text-sm;
}
.right .title {
  @apply font-bold text-3xl text-gray-800;
}
.right > div {
  @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
}
.right .line {
  @apply h-[1px] w-16 bg-gray-200;
}
</style>